package com.itheima.mall.service.sms;

import cn.hutool.core.convert.Convert;
import com.itheima.mall.api.pms.ProductServiceApi;
import com.itheima.mall.api.sms.HomeBrandServiceApi;
import com.itheima.mall.api.sms.HomeNewProductServiceApi;
import com.itheima.mall.api.sms.HomeRecommendProductServiceApi;
import com.itheima.mall.api.sms.HomeRecommendSubjectServiceApi;
import com.itheima.mall.pojo.pms.Product;
import com.itheima.mall.pojo.sms.HomeNewProduct;
import com.itheima.mall.pojo.sms.HomeRecommendProduct;
import com.itheima.mall.pojo.sms.HomeRecommendSubject;
import com.itheima.mall.pojo.sms.SmsHomeBrand;
import com.itheima.mall.vo.PageResult;
import javafx.scene.layout.BackgroundRepeat;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class HomeNewProductService {
    @DubboReference
    private HomeNewProductServiceApi homeNewProductServiceApi;
    @DubboReference
    private HomeRecommendProductServiceApi homeRecommendProductServiceApi;
    @DubboReference
    private HomeRecommendSubjectServiceApi homeRecommendSubjectServiceApi;
    @DubboReference
    private HomeBrandServiceApi homeBrandServiceApi;
    @DubboReference
    private ProductServiceApi productServiceApi;
    /**
     * 添加首页新品
     * @param
     * @return
     */
    public void newProductCreate(List<Map> list) {
        for (Map map : list) {
            Long productId = Convert.toLong(map.get("productId"));
            Product product = productServiceApi.findProductById(productId);
            HomeNewProduct homeRecommendProduct = new HomeNewProduct();
            homeRecommendProduct.setProductId(productId);
            homeRecommendProduct.setRecommendStatus(product.getRecommandStatus());
            homeRecommendProduct.setProductName(product.getName());
            homeRecommendProduct.setSort(product.getSort());
            homeNewProductServiceApi.newProductCreate(homeRecommendProduct);
        }
    }

    /**
     * 批量删除首页新品
     * 批量删除
     * @param ids
     * @return
     */
    public Boolean newProductDelete(Long[] ids) {
        Boolean result = homeNewProductServiceApi.newProductDelete(ids);
        return  result;
    }

    /**
     * 分页查询首页新品
     * @param pageNum
     * @param pageSize
     * @param productName
     * @param recommendStatus
     * @return
     */
    public PageResult<HomeNewProduct> newProductList(Integer pageNum, Integer pageSize, String productName, Integer recommendStatus) {
        PageResult<HomeNewProduct> pageResult = homeNewProductServiceApi.newProductList(pageNum,pageSize,productName,recommendStatus);
        return pageResult;
    }


    /**
     * 批量修改首页新品状态
     * @param ids
     * @param recommendStatus
     * @return
     */
    public void newProductUpdateRecommendStatus(Long[] ids, Integer recommendStatus) {
        //根据ids查找新品
        List<HomeNewProduct> homeNewProductList = homeNewProductServiceApi.findNewProductByIds(ids);
        //更新查找出的新品状态
        for (HomeNewProduct homeNewProduct : homeNewProductList) {
            homeNewProduct.setRecommendStatus(recommendStatus);
            homeNewProductServiceApi.update(homeNewProduct);
        }
    }

    public void newProductUpdateSortById(Long id, Integer sort) {
        HomeNewProduct homeNewProduct = homeNewProductServiceApi.findNewProductById(id);
        homeNewProduct.setSort(sort);
        homeNewProductServiceApi.update(homeNewProduct);
    }

    /**
     * 添加首页推荐
     * @param list
     * @return
     */
    public void recommendProductCreate(List<Map> list) {
        for (Map map : list) {
            Long productId = Convert.toLong(map.get("productId"));
            Product product = productServiceApi.findProductById(productId);
            HomeRecommendProduct homeRecommendProduct = new HomeRecommendProduct();
            homeRecommendProduct.setProductId(productId);
            homeRecommendProduct.setRecommendStatus(product.getRecommandStatus());
            homeRecommendProduct.setProductName(product.getName());
            homeRecommendProduct.setSort(product.getSort());
            homeRecommendProductServiceApi.recommendProductCreate(homeRecommendProduct);
        }
    }

    /**
     * 批量删除首页推荐
     * 批量删除
     * @param ids
     * @return
     */
    public Boolean recommendProductDelete(Long[] ids) {
        return homeRecommendProductServiceApi.RecommendProductDelete(ids);
    }

    /**
     * 分页查询首页推荐
     * @param pageNum
     * @param pageSize
     * @param productName
     * @param recommendStatus
     * @return
     */
    public PageResult<HomeRecommendProduct> recommendProductList(Integer pageNum, Integer pageSize, String productName, Integer recommendStatus) {
        PageResult<HomeRecommendProduct> pageResult = homeRecommendProductServiceApi.recommendProductList(pageNum,pageSize,productName,recommendStatus);
        return pageResult;
    }

    /**
     * 批量修改首页新品状态
     * @param ids
     * @param recommendStatus
     * @return
     */
    public void recommendProductUpdateRecommendStatus(Long[] ids, Integer recommendStatus) {
        //根据ids查找推荐
        List<HomeRecommendProduct> homeRecommendProductList = homeRecommendProductServiceApi.findRecommendProductByIds(ids);
        //更新查找出的推荐状态
        for (HomeRecommendProduct homeRecommendProduct : homeRecommendProductList) {
            homeRecommendProduct.setRecommendStatus(recommendStatus);
            homeRecommendProductServiceApi.update(homeRecommendProduct);
        }
    }

    public void recommendProductUpdateSortById(Long id, Integer sort) {
        HomeRecommendProduct homeRecommendProduct = homeRecommendProductServiceApi.findRecommendProductById(id);
        homeRecommendProduct.setSort(sort);
        homeRecommendProductServiceApi.update(homeRecommendProduct);
    }

    /**
     * 添加首页推荐专题
     * @param list
     * @return
     */
    //TODO:bug
    public void recommendSubjectCreate(List<Map> list) {
        for (Map map : list) {
            Long subjectId = Convert.toLong(map.get("subjectId"));
            //根据productId查找id详情
            Product product = productServiceApi.findProductById(subjectId);
            HomeRecommendSubject homeRecommendSubject = new HomeRecommendSubject();
            homeRecommendSubject.setSubjectId(subjectId);
            homeRecommendSubject.setRecommendStatus(product.getRecommandStatus());
            homeRecommendSubject.setSubjectName(product.getName());
            homeRecommendSubject.setSort(product.getSort());
            homeRecommendSubjectServiceApi.recommendSubjectCreate(homeRecommendSubject);
        }
    }

    /**
     * 批量删除专题
     * 批量删除
     * @param ids
     * @return
     */
    public Boolean recommendSubjectDelete(Long[] ids) {
        return homeRecommendSubjectServiceApi.RecommendSubjectDelete(ids);
    }

    /**
     * 分页查询首页推荐
     * @param pageNum
     * @param pageSize
     * @param productName
     * @param recommendStatus
     * @return
     */
    public PageResult<HomeRecommendSubject> recommendSubjectList(Integer pageNum, Integer pageSize, String productName, Integer recommendStatus) {
        PageResult<HomeRecommendSubject> pageResult = homeRecommendSubjectServiceApi.recommendSubjectList(pageNum,pageSize,productName,recommendStatus);
        return pageResult;
    }

    /**
     * 批量修改首页新品状态
     * @param ids
     * @param recommendStatus
     * @return
     */
    public void recommendSunjectUpdateRecommendStatus(Long[] ids, Integer recommendStatus) {
        //根据ids查找推荐
        List<HomeRecommendSubject> homeRecommendSubjectList = homeRecommendSubjectServiceApi.findRecommendSubjectByIds(ids);
        //更新查找出的推荐状态
        for (HomeRecommendSubject homeRecommendSubject : homeRecommendSubjectList) {
            homeRecommendSubject.setRecommendStatus(recommendStatus);
            homeRecommendSubjectServiceApi.update(homeRecommendSubject);
        }
    }

    public void recommendSubjectUpdateSortById(Long id, Integer sort) {
        HomeRecommendSubject homeRecommendSubject = homeRecommendSubjectServiceApi.findRecommendSubjectById(id);
        homeRecommendSubject.setSort(sort);
        homeRecommendSubjectServiceApi.update(homeRecommendSubject);
    }

    public void createBrand(SmsHomeBrand homeBrand) {
        homeBrandServiceApi.createBrand(homeBrand);
    }

    public Boolean deleteBrand(int[] ids) {
      return   homeBrandServiceApi.deleteBrand(ids);
    }

    public PageResult<SmsHomeBrand> getList(String brandName, Integer pageNum, Integer pageSize, Integer recommendStatus) {
        return homeBrandServiceApi.getList(brandName,pageNum,pageSize,recommendStatus);
    }

    public void updateStatus(Integer recommendStatus, int[] ids) {
        homeBrandServiceApi.updateStatus(recommendStatus,ids);
    }

    public void updateSort(Integer id, Integer sort) {
        homeBrandServiceApi.updateSort(id,sort);
    }
}
